#include "palette.h"
#include <QHBoxLayout>
#include <QGridLayout>

Palette::Palette(QWidget *parent)
    : QDialog(parent)
{
    createCtrlFrame();
    createcontentFrame();
    QHBoxLayout *mainLayout = new QHBoxLayout(this);
    mainLayout -> addWidget(ctrlFrame);
    mainLayout -> addWidget(contentFrame);
}

//创建颜色选择区
void Palette::createCtrlFrame(){
    ctrlFrame = new QFrame;   //颜色选择面板
    windowLabel = new QLabel(tr("QPalette::Window:"));
    windowComboBox = new QComboBox;  //创建一个QComboBox对象
    fillColorList(windowComboBox);  //a
    connect(windowComboBox,SIGNAL(activated(int)),this,SLOT(ShowWindow())); //b

    windowTextLabel = new QLabel(tr("QPalette::windowText:"));
    windowTextComboBox = new QComboBox;
    fillColorList(windowTextComboBox);
    connect(windowTextComboBox,SIGNAL(activated(int)),this,SLOT(ShowWindowText()));

    buttonLabel =new QLabel(tr("QPalette::Button: "));
    buttonComboBox =new QComboBox;
    fillColorList(buttonComboBox);
    connect(buttonComboBox,SIGNAL(activated(int)),this,SLOT(ShowButton()));

    buttonTextLabel =new QLabel(tr("QPalette::ButtonText: "));
    buttonTextComboBox =new QComboBox;
    fillColorList(buttonTextComboBox);
    connect(buttonTextComboBox,SIGNAL(activated(int)),this,SLOT(ShowButtonText()));

    baseLabel =new QLabel(tr("QPalette::Base: "));
    baseComboBox =new QComboBox;
    fillColorList(baseComboBox);
    connect(baseComboBox,SIGNAL(activated(int)),this,SLOT(ShowBase()));

    QGridLayout *mainLayout = new QGridLayout(ctrlFrame);
    mainLayout->setSpacing(20);
    mainLayout->addWidget(windowLabel,0,0);
    mainLayout->addWidget(windowComboBox,0,1);
    mainLayout->addWidget(windowTextLabel,1,0);
    //mainLayout->addWidget(windowTextComboBox,0,3);  //有改动
    mainLayout->addWidget(windowTextComboBox,1,1);
    mainLayout->addWidget(buttonLabel,2,0);
    mainLayout->addWidget(buttonComboBox,2,1);
    mainLayout->addWidget(buttonTextLabel,3,0);
    mainLayout->addWidget(buttonTextComboBox,3,1);
    mainLayout->addWidget(baseLabel,4,0);
    mainLayout->addWidget(baseComboBox,4,1);
}

//变颜色，窗体外观变
void Palette::createcontentFrame(){
    contentFrame = new QFrame;   //具体显示面板
    label1 = new QLabel(tr("请选择一个值："));
    comboBox1 = new QComboBox;
    label2 = new QLabel(tr("请输入字符串："));
    lineEdit2 = new QLineEdit;
    textEdit = new QTextEdit;
    QGridLayout *TopLayout = new QGridLayout;
    TopLayout ->addWidget(label1,0,0);
    TopLayout -> addWidget(comboBox1,0,1);
    TopLayout -> addWidget(label2,1,0);
    TopLayout->addWidget(lineEdit2,1,1);
    TopLayout->addWidget(textEdit,2,0,1,2);

    OkBtn = new QPushButton(tr("确认"));
    CancelBtn = new QPushButton(tr("取消"));
    QHBoxLayout *BottomLayout = new QHBoxLayout;
    BottomLayout -> addStretch(1);
    BottomLayout -> addWidget(OkBtn);
    BottomLayout -> addWidget(CancelBtn);
    QVBoxLayout *mainLayout = new QVBoxLayout(contentFrame);
    mainLayout -> addLayout(TopLayout);
    mainLayout -> addLayout(BottomLayout);
}

//响应对背景颜色的选择
void Palette::ShowWindow(){
    //获得当前选择的颜色值
    QStringList colorList = QColor::colorNames();
    QColor color = QColor(colorList[windowComboBox -> currentIndex()]);
    QPalette p = contentFrame -> palette();  //a
    p.setColor(QPalette::Window,color);  //b
    //把修改后的调色板信息应用到contentFrame窗体中，更新显示
    contentFrame -> setPalette(p);
    contentFrame -> update();
}

//前景色设置
void Palette::ShowWindowText(){
    QStringList colorList = QColor::colorNames();
    QColor color = QColor(colorList[windowComboBox -> currentIndex()]);
    QPalette p = contentFrame -> palette();  //a
    p.setColor(QPalette::WindowText,color);
    contentFrame -> setPalette(p);
}

//按钮背景色
void Palette::ShowButton(){
    QStringList colorList = QColor::colorNames();
    QColor color = QColor(colorList[windowComboBox -> currentIndex()]);
    QPalette p = contentFrame -> palette();  //a
    p.setColor(QPalette::Button,color);
    contentFrame -> setPalette(p);
    contentFrame -> update();
}

//按钮文字颜色
void Palette::ShowButtonText(){
    QStringList colorList = QColor::colorNames();
    QColor color = QColor(colorList[windowComboBox -> currentIndex()]);
    QPalette p = contentFrame -> palette();  //a
    p.setColor(QPalette::ButtonText,color);
    contentFrame -> setPalette(p);
}

//文本框背景色
void Palette::ShowBase(){
    QStringList colorList = QColor::colorNames();
    QColor color = QColor(colorList[windowComboBox -> currentIndex()]);
    QPalette p = contentFrame -> palette();  //a
    p.setColor(QPalette::Base,color);
    contentFrame -> setPalette(p);
}

//插入颜色
void Palette::fillColorList(QComboBox *comboBox){
    QStringList colorList = QColor::colorNames(); //获得Qt所有知道名称的颜色名列表，返回的是一个字符串列表colorList
    QString color; //b新建一个QString对象，为循环遍历做准备
    foreach (color, colorList) { //对颜色名列表遍历
        QPixmap pix(QSize(70,20));  //c新建一个QPixmap对象pix作为显示颜色的图标
        pix.fill(QColor(color));  //为pix填充当前遍历的颜色
        comboBox -> addItem(QIcon(pix),NULL); //d调用QComboBox的addItem()函数为下拉列表框插入一个条目，并以准备好的pix作为插入条目的图标，名称设为NULL，即不显示颜色的名称
        comboBox -> setIconSize(QSize(70,20));  //e设置图标的尺寸，图标默认尺寸是一个方形，将它设置为与pix相同尺寸的长方形
        comboBox -> setSizeAdjustPolicy(QComboBox::AdjustToContents); //f设置下拉列表框的尺寸调整策略为AdjustToContents（符合内容的大小）
    }
}

Palette::~Palette()
{

}
